package com.runfast.waimai.service.impl;


import com.runfast.common.service.BaseService;
import com.runfast.waimai.dao.mapper.RunfastGoodsSellRecordMapper;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class RunfastGoodsSellRecordServiceImpl extends BaseService<RunfastGoodsSellRecord, Integer, RunfastGoodsSellRecordExample> implements RunfastGoodsSellRecordService {
    public List<RunfastGoodsSellRecord> selectJieDanAndPeiSongByMap(Map<Object,Object> map){
        RunfastGoodsSellRecordMapper runfastGoodsSellRecordMapper=(RunfastGoodsSellRecordMapper)this.mapper;
        return runfastGoodsSellRecordMapper.selectJieDanAndPeiSongByMap(map);
    }
    public Integer countJieDanOrPeiSongOrTotalByMap(Map<Object,Object> map){
        RunfastGoodsSellRecordMapper runfastGoodsSellRecordMapper=(RunfastGoodsSellRecordMapper)this.mapper;
        return runfastGoodsSellRecordMapper.countJieDanOrPeiSongOrTotalByMap(map);
    }
    public List<RunfastGoodsSellRecord> selectByMapWithPageable(Map<Object,Object> map){
        RunfastGoodsSellRecordMapper runfastGoodsSellRecordMapper=(RunfastGoodsSellRecordMapper)this.mapper;
        return runfastGoodsSellRecordMapper.selectByMapWithPageable(map);
    }
    public Integer countByMap(Map<Object,Object> map){
        RunfastGoodsSellRecordMapper runfastGoodsSellRecordMapper=(RunfastGoodsSellRecordMapper)this.mapper;
        return runfastGoodsSellRecordMapper.countByMap(map);
    }
    /**调度台订单信息*/
    public List<RunfastGoodsSellRecord> selectByMapDiaoDu(Map<Object,Object> map){
        RunfastGoodsSellRecordMapper runfastGoodsSellRecordMapper=(RunfastGoodsSellRecordMapper)this.mapper;
        return runfastGoodsSellRecordMapper.selectByMapDiaoDu(map);
    }
    /**调度台订单信息*/
    public Integer countByMapDiaoDu(Map<Object,Object> map){
        RunfastGoodsSellRecordMapper runfastGoodsSellRecordMapper=(RunfastGoodsSellRecordMapper)this.mapper;
        return runfastGoodsSellRecordMapper.countByMapDiaoDu(map);
    }

    public Map<Object,Object> cityData(Map<Object,Object> map){
        RunfastGoodsSellRecordMapper runfastGoodsSellRecordMapper=(RunfastGoodsSellRecordMapper)this.mapper;
        return runfastGoodsSellRecordMapper.cityData(map);
    }




    /**以下为孟祥的代码*/
    @Resource
    RunfastGoodsSellRecordMapper dao;
    @Override
    public List<Map<String,String>>  findByAgentnameAndbusinessnameAndTime(List agentid, String name, Date date1, Date date2, Integer isdelive, Integer page, Integer pagesize) {
        return dao.findByAgentnameAndbusinessnameAndTime(agentid, name, date1, date2,isdelive,page,pagesize);
    }
    @Override
    public Integer findByAgentnameAndbusinessnameAndTimecount(List agentid, String name, Date date1, Date date2, Integer isdelive, Integer page, Integer pagesize) {
        return dao.findByAgentnameAndbusinessnameAndTimecount(agentid, name, date1, date2,isdelive,page,pagesize);
    }
    @Override
    public List<Map<String,String>> findAgentnameMoney(List agentId,Date createtime1, Date createatime2, Integer isdeliver,Integer page,Integer pages) {
        return dao.findAgentnameMoney(agentId,createtime1, createatime2,isdeliver,page,pages);
    }
    @Override
    public List<Map<String, BigDecimal>> findshopperAndCityAndtime(String cityname, String shoppername, Date date1, Date date2, Integer page, Integer pagesize, List agentid) {
        return dao.findshopperAndCityAndtime(cityname, shoppername, date1, date2, page,pagesize,agentid);
    }
    @Override
    public List<RunfastGoodsSellRecord> findaftertime(Date date1, Date date2, String cityname, String shoppername) {
        return dao.findaftertime(date1, date2, cityname, shoppername);
    }
    @Override
    public List<RunfastGoodsSellRecord> finderrorOver(Date date1, Date date2, String cityname, String shoppername) {
        return dao.finderrorOver(date1, date2, cityname, shoppername);
    }
    @Override
    public List<RunfastGoodsSellRecord> findlastscore(Date date1, Date date2, String cityname, String shoppername) {
        return dao.findlastscore(date1, date2, cityname, shoppername);
    }
    @Override
    public Integer findAgentnameMoneycount(List agentId,Date date1, Date date2, Integer isdelive, Integer page1, Integer pages) {
        return dao.findAgentnameMoneycount(agentId, date1, date2, isdelive, page1, pages);
    }
    @Override
    public Integer findshopperAndCityAndtimecount(String cityname, String shoppername, Date date1, Date date2, Integer page, Integer pagesize, List agentid) {
        return dao.findshopperAndCityAndtimecount(cityname, shoppername,  date1, date2, page,  pagesize, agentid);
    }

    @Override
    public List<RunfastGoodsSellRecord> findBybussinessWithdraw(Integer userid) {
        return dao.findBybussinessWithdraw(userid);
    }

    @Override
    public List<Map<String,Object>> selectSalesCountExport(Map<String, Object> mapIn) {
        RunfastGoodsSellRecordMapper mapper=(RunfastGoodsSellRecordMapper)this.mapper;
        List<Map<String, Object>> list = mapper.selectSalesCountExport(mapIn);
        if (list != null && list.size() > 0) {
            for (Map<String, Object> map : list) {
                Double jin = ((BigDecimal)map.get("jin")).doubleValue();
                Double busget = ((BigDecimal)map.get("busget")).doubleValue();
                Double agentget = ((BigDecimal)map.get("agentget")).doubleValue();//平台佣金
                Double agentBusget = ((BigDecimal)map.get("agentBusget")).doubleValue();//代理商佣金
                Double subsidy = ((BigDecimal)map.get("subsidy")).doubleValue();//商家补贴配送费金额
                if (jin == null) {
                    jin = 0.0;
                }
                if (busget == null) {
                    busget = 0.0;
                }
                if (subsidy == null) {
                    subsidy = 0.0;
                }
                Double dls = (Double) map.get("dls");//代理商优惠券金额
                Double pt = (Double) map.get("pt");//平台优惠券金额
                if (dls == null) {
                    dls = 0.0;
                }
                if (pt == null) {
                    pt = 0.0;
                }
                agentget = agentget - pt;
                agentBusget = agentBusget - dls;
                DecimalFormat df = new DecimalFormat("##0.00");
                map.put("agentget", df.format(agentget));//平台佣金
                map.put("busget", df.format(agentBusget));//代理商佣金
                Double ticheng = jin - busget - subsidy;
                map.put("bus", df.format(ticheng));
            }
        }
        return list;
    }
    @Override
    public List<Map<String, Object>> businessSalesCount(Map<String, Object> mapIn) {
        RunfastGoodsSellRecordMapper mapper=(RunfastGoodsSellRecordMapper)this.mapper;
        List<Map<String, Object>> list = mapper.businessSalesCount(mapIn);
        //算提成
        if (list != null && list.size() > 0) {
            for (Map<String, Object> map : list) {
                Double jin = ((BigDecimal)map.get("jin")).doubleValue();
                Double busget = ((BigDecimal)map.get("busget")).doubleValue();
                Double agentget = ((BigDecimal)map.get("agentget")).doubleValue();//平台佣金
                Double agentBusget = ((BigDecimal)map.get("agentBusget")).doubleValue();//代理商佣金
                if (jin == null) {
                    jin = 0.0;
                }
                if (busget == null) {
                    busget = 0.0;
                }
                if (agentget == null) {
                    agentget = 0.0;
                }
                Double dls = (Double) map.get("dls");//代理商优惠券金额
                Double pt = (Double) map.get("pt");//平台优惠券金额
                if (dls == null) {
                    dls = 0.0;
                }
                if (pt == null) {
                    pt = 0.0;
                }
                agentget = agentget - pt;
                agentBusget = agentBusget - dls;
                DecimalFormat df = new DecimalFormat("##0.00");
                Double tprice = ((BigDecimal)map.get("tprice")).doubleValue();//订单总额
                if (tprice == null) {
                    tprice = 0.0;
                }
                map.put("agentget", df.format(agentget));//平台佣金
                map.put("tprice", df.format(tprice));//订单总额
                Double ticheng = jin - busget;
                map.put("busget", agentBusget + "");//代理商佣金
                map.put("bus", df.format(ticheng));//商家收入

            }
        }
        return list;
    }
    @Override
    public Integer countByBusinessSalesCount(RunfastGoodsSellRecordExample example) {
        RunfastGoodsSellRecordMapper mapper=(RunfastGoodsSellRecordMapper)this.mapper;
        return mapper.countByBusinessSalesCount(example);
    }

}